//
//  AdressBookVC.m
//  AdressBook
//
//  Created by 周成龙 on 16/11/5.
//  Copyright © 2016年 ZCL. All rights reserved.
//

#import "AdressBookVC.h"

#import <AddressBook/AddressBook.h>

@interface AdressBookVC ()

@end

@implementation AdressBookVC

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    //1. 获取通讯录状态
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    //2. 如果用户已经授权
    if (status != kABAuthorizationStatusAuthorized) return;
    //3. 创建通讯录对象
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    //4. 从通讯录对象中，将所有联系人拷贝出来
    CFArrayRef phones = ABAddressBookCopyArrayOfAllPeople(addressBook);
    //5. 遍历所有的联系人（每个联系人都是一条数据）
    CFIndex peopleCount = CFArrayGetCount(phones);
    for (CFIndex i = 0; i < peopleCount; i ++) {
        //6. 获取到联系人
        ABRecordRef people = CFArrayGetValueAtIndex(phones, i);
        //7. 获取姓名
        NSString *firstName = CFBridgingRelease(ABRecordCopyValue(people, kABPersonFirstNameProperty));
        NSString *lastName = CFBridgingRelease(ABRecordCopyValue(people, kABPersonLastNameProperty));
        NSLog(@"%@-%@", firstName, lastName);
        
        //8. 获取联系人电话
        NSString *value = CFBridgingRelease(ABRecordCopyValue(people, kABPersonPhoneProperty));
        NSLog(@"%@-", value);
    }
    
    CFRelease(phones);
}



@end
